//
//  NSString+URLEncoding.m
//  MBA
//
//  Created by 一览iOS on 14-5-8.
//  Copyright (c) 2014年 job1001. All rights reserved.
//

#import "NSString+URLEncoding.h"

@implementation NSString (URLEncoding)

- (NSString *)URLEncodedForString
{
    NSString *result = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                            (CFStringRef)self,
                                            NULL,
                                            CFSTR("!*'();:@&=+$,/?%#[] "),
                                            kCFStringEncodingUTF8));
    return result;
}

- (NSString*)URLDecodedForString
{
    NSString *result = (NSString *)
    CFBridgingRelease(CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
                                                            (CFStringRef)self,
                                                            CFSTR(""),
                                                            kCFStringEncodingUTF8));
    if (result){
        return result;
    }
    return [self stringByReplacingPercentEscapesUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)];
}
-(NSString *)utf8ToUnicode
{
    NSString *string = self;
    NSUInteger length = [string length];
    NSMutableString *s = [NSMutableString stringWithCapacity:0];
    for (int i = 0;i < length; i++)
    {
        unichar _char = [string characterAtIndex:i];
        //判断是否为英文和数字
        if (_char <= '9' && _char >='0')
        {
            [s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]];
        }
        else if(_char >='a' && _char <= 'z')
        {
            [s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]]; 
        }
        else if(_char >='A' && _char <= 'Z')
        {
            [s appendFormat:@"%@",[string substringWithRange:NSMakeRange(i,1)]];   
        }
        else
        {
            NSString * str =  [NSString stringWithFormat:@"0000%x",[string characterAtIndex:i]];
            NSRange range;
            range.length = 4;
            range.location = [str length]-4;
            str = [str substringWithRange:range];
            [s appendFormat:@"\\u%@",str];
        }
    }
    return s;
}

-(NSDictionary *)stringWithDic{
    if ([self containsString:@"?"]) {
        NSString *usefulUrl = [[self componentsSeparatedByString:@"?"] lastObject];
        if ([usefulUrl containsString:@"&"]) {
            NSArray *urlArr = [usefulUrl componentsSeparatedByString:@"&"];
            
            if ([urlArr isKindOfClass: [NSArray class]] && urlArr.count > 0) {
                NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
                for (NSString *url in urlArr) {
                    if ([url containsString:@"="]) {
                        NSArray *cutUrl = [url componentsSeparatedByString:@"="];
                        NSString *content = [cutUrl lastObject];
                        if ([content isEqualToString:@"null"]) {
                            content = @"";
                        }
                        [dic setObject:content  forKey:[cutUrl firstObject]];
                    }
                }
                if (dic.count > 0) {
                    return dic;
                }
            }
        }
    }
    return nil;
}

-(NSMutableDictionary *)getUrlDic{
    if ([self containsString:@"?"]) {
        NSString *usefulUrl = [[self componentsSeparatedByString:@"?"] lastObject];
        if ([usefulUrl containsString:@"&"]) {
            NSArray *urlArr = [usefulUrl componentsSeparatedByString:@"&"];
            if (urlArr && urlArr.count > 0) {
                NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
                for (NSString *url in urlArr) {
                    if ([url containsString:@"="]) {
                        NSArray *cutUrl = [url componentsSeparatedByString:@"="];
                        [dic setObject:[cutUrl lastObject] forKey:[cutUrl firstObject]];
                    }
                }
                if (dic.count > 0) {
                    return dic;
                }
            }
        }
    }
    return nil;
}

-(NSString *)getUrlFileNameWithKey:(NSString *)key{
    NSString *name = [self lastPathComponent];
    if ([name containsString:@"?"]) {
        name = [[name componentsSeparatedByString:@"?"] firstObject];
    }
    if ([self containsString:@"http://"] || [self containsString:@"https://"]) {
        return [NSString stringWithFormat:@"%@%@",key,name];
    }
    return name;
    
}

@end
